package org.tabletopmachine.states.mainmenu;

import org.tabletopmachine.Game;
import org.tabletopmachine.IMenu;
import org.tabletopmachine.basics.Conversions;
import org.tabletopmachine.match.Match;
import org.tabletopmachine.match.Player;
import org.tabletopmachine.match.unit.Unit;
import de.matthiasmann.twl.Button;
import de.matthiasmann.twl.DialogLayout;
import de.matthiasmann.twl.EditField;
import de.matthiasmann.twl.Label;
import de.matthiasmann.twl.ListBox;
import de.matthiasmann.twl.Widget;
import de.matthiasmann.twl.model.SimpleChangableListModel;

public class NewGameMenu extends Widget implements IMenu {
	private Match _match;

	private final MainMenuState _mMS;

	// UI Elements for Top Player
	private final EditField _topNameEditField;
	private final Label _topArmyLabel;
	private final Label _topUnitsLabel;
	private final Button _topUnitsButton;
	private final Label _topBorderColorLabel;
	private final Label _topFillColorLabel;
	private final EditField _topArmyEditField;
	private final ListBox<Unit> _topUnitsListBox;
	private final Button _topBorderColorButton;
	private final Button _topFillColorButton;

	// UI Elements for Bottom Player
	private final EditField _botNameEditField;
	private final Label _botArmyLabel;
	private final Label _botUnitsLabel;
	private final Button _botUnitsButton;
	private final Label _botBorderColorLabel;
	private final Label _botFillColorLabel;
	private final EditField _botArmyEditField;
	private final ListBox<Unit> _botUnitsListBox;
	private final Button _botBorderColorButton;
	private final Button _botFillColorButton;

	private final DialogLayout _newGamePanel;

	private final Button _startButton;
	private final Button _cancelButton;

	public NewGameMenu(MainMenuState mainMenuState) {
		_mMS = mainMenuState;

		_newGamePanel = new DialogLayout();

		// Create UI Elements for Top Player configuration
		_topNameEditField = new EditField();
		_topArmyLabel = new Label();
		_topUnitsLabel = new Label();
		_topUnitsButton = new Button();
		_topBorderColorLabel = new Label();
		_topFillColorLabel = new Label();
		_topArmyEditField = new EditField();
		_topUnitsListBox = new ListBox<Unit>();
		_topBorderColorButton = new Button();
		_topFillColorButton = new Button();

		// Create UI Elements for Bottom Player configuration
		_botNameEditField = new EditField();
		_botArmyLabel = new Label();
		_botUnitsLabel = new Label();
		_botUnitsButton = new Button();
		_botBorderColorLabel = new Label();
		_botFillColorLabel = new Label();
		_botArmyEditField = new EditField();
		_botUnitsListBox = new ListBox<Unit>();
		_botBorderColorButton = new Button();
		_botFillColorButton = new Button();

		// Create other UI Elements

		_startButton = new Button();
		_cancelButton = new Button();

		// Set up start,cancel buttons

		_startButton.setText("Start");
		_cancelButton.setText("Cancel");

		// Set up configuration
		_topArmyLabel.setText("Army");
		_topUnitsLabel.setText("Units");
		_topUnitsButton.setText("Manage");
		_topBorderColorLabel.setText("Border Color");
		_topFillColorLabel.setText("Fill Color");

		_botArmyLabel.setText("Army");
		_botUnitsLabel.setText("Units");
		_botUnitsButton.setText("Manage");
		_botBorderColorLabel.setText("Border Color");
		_botFillColorLabel.setText("Fill Color");

		_topNameEditField.setText("Top Player");
		_botNameEditField.setText("Bottom Player");

		_topBorderColorButton.setText("Black");
		_topBorderColorButton.setTheme("coloredbutton");
		_botBorderColorButton.setText("Black");
		_botBorderColorButton.setTheme("coloredbutton");
		_topFillColorButton.setText("Red");
		_topFillColorButton.setTheme("coloredbutton");
		_botFillColorButton.setText("Blue");
		_botFillColorButton.setTheme("coloredbutton");

		_topUnitsListBox.setModel(new SimpleChangableListModel<Unit>());
		_botUnitsListBox.setModel(new SimpleChangableListModel<Unit>());

		// Create Layout
		DialogLayout.Group TopPlayerNameField = _newGamePanel.createParallelGroup(_topNameEditField);
		DialogLayout.Group TopPlayerLabels = _newGamePanel.createParallelGroup(_topArmyLabel, _topUnitsLabel, _topUnitsButton, _topBorderColorLabel,
				_topFillColorLabel);
		DialogLayout.Group TopPlayerFields = _newGamePanel.createParallelGroup(_topArmyEditField, _topUnitsListBox, _topBorderColorButton, _topFillColorButton);

		DialogLayout.Group BotPlayerNameField = _newGamePanel.createParallelGroup(_botNameEditField);
		DialogLayout.Group BotPlayerLabels = _newGamePanel.createParallelGroup(_botArmyLabel, _botUnitsLabel, _botUnitsButton, _botBorderColorLabel,
				_botFillColorLabel);
		DialogLayout.Group BotPlayerFields = _newGamePanel.createParallelGroup(_botArmyEditField, _botUnitsListBox, _botBorderColorButton, _botFillColorButton);

		_newGamePanel.setHorizontalGroup(_newGamePanel
				.createSequentialGroup()
				.addGroup(
						_newGamePanel.createParallelGroup().addGroup(TopPlayerNameField)
								.addGroup(_newGamePanel.createSequentialGroup(TopPlayerLabels, TopPlayerFields)))
				.addGroup(
						_newGamePanel.createParallelGroup().addGroup(BotPlayerNameField)
								.addGroup(_newGamePanel.createSequentialGroup(BotPlayerLabels, BotPlayerFields))
								.addGroup(_newGamePanel.createSequentialGroup(_cancelButton, _startButton))));

		_newGamePanel.setVerticalGroup(_newGamePanel
				.createSequentialGroup()
				.addGroup(_newGamePanel.createParallelGroup(_topNameEditField, _botNameEditField))
				.addGroup(_newGamePanel.createParallelGroup(_topArmyLabel, _topArmyEditField, _botArmyLabel, _botArmyEditField))
				.addGroup(
						_newGamePanel.createParallelGroup().addGroup(_newGamePanel.createSequentialGroup(_topUnitsLabel, _topUnitsButton))
								.addWidget(_topUnitsListBox).addGroup(_newGamePanel.createSequentialGroup(_botUnitsLabel, _botUnitsButton))
								.addWidget(_botUnitsListBox))
				.addGroup(_newGamePanel.createParallelGroup(_topBorderColorLabel, _topBorderColorButton, _botBorderColorLabel, _botBorderColorButton))
				.addGroup(_newGamePanel.createParallelGroup(_topFillColorLabel, _topFillColorButton, _botFillColorLabel, _botFillColorButton))
				.addGroup(_newGamePanel.createParallelGroup().addWidgets(_cancelButton, _startButton)));

		_startButton.addCallback(new Runnable() {
			@Override
			public void run() {
				start();
			}
		});

		_cancelButton.addCallback(new Runnable() {
			@Override
			public void run() {
				cancel();
			}
		});
		_topUnitsButton.addCallback(new Runnable() {
			public void run() {
				_mMS.enterManageArmyMenu(Game.getMatch().getPlayer1());
			}
		});
		_botUnitsButton.addCallback(new Runnable() {
			public void run() {
				_mMS.enterManageArmyMenu(Game.getMatch().getPlayer2());
			}

		});
		// Add panel to screen
		add(_newGamePanel);

	}

	private void start() {
		Game.getGameInstance().enterState(_mMS.getID() + 1);
	}

	private void cancel() {
		_mMS.enterMainMenu();
	}

	protected void layout() {
		// set screen to max size
		setSize(900, 600);
		_newGamePanel.adjustSize();
		_newGamePanel.setPosition(getInnerX() + (getInnerWidth() - _newGamePanel.getWidth()) / 2, getInnerY() + (getInnerHeight() - _newGamePanel.getHeight())
				/ 2);
	}

	public void update() {
		if (isVisible()) {
			if (_topNameEditField.hasKeyboardFocus()) {
				if (_topNameEditField.getText().equals("Top Player"))
					_topNameEditField.setText("");
			} else {
				if (_topNameEditField.getText().equals(""))
					_topNameEditField.setText("Top Player");
			}

			if (_botNameEditField.hasKeyboardFocus()) {
				if (_botNameEditField.getText().equals("Bottom Player"))
					_botNameEditField.setText("");
			} else {
				if (_botNameEditField.getText().equals(""))
					_botNameEditField.setText("Bottom Player");
			}

			_topBorderColorButton.setOverlay(_topBorderColorButton.getOverlay().createTintedVersion(
					Conversions.ConvertSlickToTwlColor(_match.getPlayer1().getUnitBorderColor())));
			_topFillColorButton.setOverlay(_topFillColorButton.getOverlay().createTintedVersion(
					Conversions.ConvertSlickToTwlColor(_match.getPlayer1().getUnitFillColor())));

			_botBorderColorButton.setOverlay(_botBorderColorButton.getOverlay().createTintedVersion(
					Conversions.ConvertSlickToTwlColor(_match.getPlayer2().getUnitBorderColor())));
			_botFillColorButton.setOverlay(_botFillColorButton.getOverlay().createTintedVersion(
					Conversions.ConvertSlickToTwlColor(_match.getPlayer2().getUnitFillColor())));

			Player Top = Game.getMatch().getPlayer1();
			Player Bottom = Game.getMatch().getPlayer2();

			Top.setName(_topNameEditField.getText());
			Top.setArmyName(_topArmyEditField.getText());

			Bottom.setName(_botNameEditField.getText());
			Bottom.setArmyName(_botArmyEditField.getText());
		}
	}

	public void init() {
		_match = Game.getMatch();
		_topNameEditField.setText(_match.getPlayer1().getName());
		_botNameEditField.setText(_match.getPlayer2().getName());

		_topArmyEditField.setText(_match.getPlayer1().getArmyName());
		_botArmyEditField.setText(_match.getPlayer2().getArmyName());
	}

	public void loadUnits() {

		SimpleChangableListModel<Unit> modelTop = (SimpleChangableListModel<Unit>) _topUnitsListBox.getModel();
		for (Unit unit : Game.getMatch().getPlayer1().getUnits()) {
			modelTop.addElement(unit);
		}
		SimpleChangableListModel<Unit> modelBot = (SimpleChangableListModel<Unit>) _botUnitsListBox.getModel();
		for (Unit unit : Game.getMatch().getPlayer2().getUnits()) {
			modelBot.addElement(unit);
		}
	}
}
